<!-- include tpl=cart/header title=结算信息 -->
<script type="text/javascript">
var user_id = '{$session.user_id ? $session.user_id : 0}';
function check_it()
{
	var act = $.dialog.tips('正在创建订单，请稍候…',100);
	$("#saveorder").ajaxSubmit({
		'url':api_url('order','create'),
		'type':'post',
		'dataType':'json',
		'success':function(rs){
			if(rs.status){
				act.content('订单创建成功，订单号是：'+rs.info.sn);
				var ext = parseInt(user_id) > 0 ? 'id='+rs.info.id : 'sn='+rs.info.sn+"&passwd="+rs.info.passwd;
				var extOrder = ext;
				var payment = $("input[name=payment]:checked").val();
				if(payment){
					ext += '&payment='+payment.toString();
					$("input[data-name=integral]").each(function(i){
						var name = $(this).attr('data-key');
						var val = $(this).val();
						if(parseInt(val) > 0){
							ext += "&integral_val["+name+"]="+val;
						}
					});
					url = api_url('payment','create',ext);
					$.phpok.json(url,function(rs){
						if(!rs.status){
							$.dialog.alert(rs.info,function(){
								$.phpok.go(get_url('order','payment',extOrder));
							});
							return false;
						}
						if(!rs.info){
							$.phpok.go(get_url('order','info',extOrder));
							return false;
						}
						url = get_url('payment','action','id='+rs.info);
						$.phpok.go(url);
					});
					return true;
				}
				var url = get_url('order','payment',ext);
				$.phpok.go(url);
				return true;
			}
			act.close();
			$.dialog.alert(rs.info);
			return false;
		}
	});
	return false;
}
function load_freight()
{
	var id = new Array();
	$("input[data-name=product]").each(function(i){
		id.push($(this).val());
	});
	var url = api_url('cart','pricelist','ids='+id.join(","));
	if($("input[name=address_id]").length > 0){
		var address_id = $("input[name=address_id]:checked").val();
		if(address_id){
			url += "&address_id="+address_id;
		}
	}else{
		var province = $("#pca_p").val();
		var city = $("#pca_c").val();
		if(province && city){
			url += "&province="+$.str.encode(province)+"&city="+$.str.encode(city);
		}
	}
	$.phpok.json(url,function(rs){
		if(rs.status){
			var list = rs.info;
			var html = '<table class="am-table am-table-centered">';
			var total = 0;
			for(var i in list){
				html += '<tr>';
				html += '<td class="am-text-right">';
				html += '<input type="hidden" name="ext_price['+list[i].identifier+']" id="ext_price_'+list[i].identifier+'" value="'+list[i].price_val+'" />';
				html += list[i].title+'：</td>';
				html += '<td id="'+list[i].identifier+'_price" class="am-text-danger am-text-left">'+list[i].price+'</td>';
				html += '</tr>';
				total += parseFloat(list[i].price_val);
			}
			html += '<tr>';
			html += '<td class="am-text-right">总价：</td>';
			html += '<td id="all_price" class="am-text-danger am-text-left" style="width:150px;">'+total.toFixed(2)+'元</td>';
			html += '</tr>';
			html += '</table>';
			$("#price_info").html(html);
			$("#price_info_panel").show();
			return true;
		}
		$("#price_info_panel").hide();
		$.dialog.alert(rs.info);
		return false;
	})
}
function update_coupon()
{
	var code = $("#coupon").val();
	if(!code){
		$.dialog.alert('优惠码不能为空');
		return false;
	}
	var url = api_url('coupon','use','code='+$.str.encode(code));
	var tip = $.dialog.tips('正在检测优惠码，请稍候…',100).lock();
	$.phpok.json(url,function(rs){
		tip.close();
		if(!rs.status){
			$.dialog.alert(rs.info);
			return false;
		}
		$.dialog.tips('请稍候，正在刷新页面…',function(){
			$.phpok.reload();
		}).lock().time(2);
		return true;
	})
}
</script>
<section class="container pt-4">
	<input type="hidden" name="is_virtual" id="is_virtual" value="{$is_virtual ? 1 : 0}" />
	<!-- if !$session.user_id -->
	<div class="card mb-3">
		<div class="card-header">
			友情提示：我们强烈建议成为我们的用户再下单
		</div>
		<div class="card-body">
			<div class="p-4 m-4 text-center">
				<input type="button" value="您还未登录，请先登录" onclick="$.login.open()" class="btn btn-secondary" />
				<a href="{url ctrl=register/}" target="_blank" class="btn btn-primary">还没有注册，请先注册</a>
			</div>
		</div>
	</div>
	<!-- /if -->
	<form method="post" id="saveorder" onsubmit="return check_it()" class="am-form am-form-horizontal">
	<!-- if $is_virtual -->
	<div class="card">
		<div class="card-header">
			收件人信息
		</div>
		<div class="card-body">
			<div class="form-group">
				<label for="email">Email</label>
				<input type="email" class="form-control" id="email" name="email" placeholder="输入你的电子邮件" value="{$address.email}"/>
			</div>
			<div class="form-group">
				<label for="mobile">手机号</label>
				<input type="tel" class="form-control" id="mobile" name="mobile" placeholder="填写手机号" value="{$address.mobile}"/>
			</div>
		</div>
	</div>
	<!-- else -->
		<!-- include tpl=cart/checkout-address -->
	<!-- /if -->
	<div class="card mt-3">
		<div class="card-header">
			产品信息
		</div>
		<table class="table">
			<thead>
				<tr>
					<th>图片</th>
					<th>名称</th>
					<th width="80px">数量</th>
					<th width="150px">售价</th>
				</tr>
			</thead>
			<tbody>
				<!-- loop from=$rslist key=$key value=$value id=$tmpid -->
				<tr>
					<td><input type="hidden" name="id[]" data-name="product" value="{$value.id}" /><img src="{$value.thumb ? $value.thumb : 'images/nopic.png'}" width="80px" border="0" alt="{$value.title}" /></td>
					<td>
						<div><b>{$value.title}</b></div>
						<!-- loop from=$value._attrlist key=$k value=$v id=$tmpid -->
						<div>{$v.title}：{$v.content}</div>
						<!-- /loop -->
						<div>单价：{func price_format $value.price $value.currency_id}</div>
					</td>
					<td>{$value.qty}</td>
					<td class="text-danger">{func price_format $value.price*$value.qty $value.currency_id}</td>
				</tr>
				<!-- /loop -->
			</tbody>
		</table>
	</div>
	<div class="card mt-3">
		<div class="card-header">
			优惠码
		</div>
		<div class="card-body">
			<div class="row">
				<div class="col-sm-3"><input type="text" name="coupon" class="form-control" id="coupon" value="{$coupon_code}" /></div>
				<div class="col-sm-1"><input type="button" value="提交优惠码" onclick="update_coupon()" class="btn btn-primary" /></div>
				<div class="col-sm-8" id="coupon_tips"></div>
			</div>
		</div>
	</div>
	<div class="card mt-3">
		<div class="card-header">
			留言，填写您对购买的商品特别要求，不能超过80字
		</div>
		<div class="card-body">
			
			<textarea name="note" id="note" class="form-control" style="resize:none;"></textarea>
		</div>
	</div>
	<div class="card mt-3">
		<div class="card-header">
			信息汇总
		</div>
		<table class="table am-table-centered">
			<tbody>
				<!-- loop from=$pricelist key=$key value=$value id=$tmpid -->
				<tr>
					<td class="text-right">
						<input type="hidden" name="ext_price[{$value.identifier}]" id="ext_price_{$value.identifier}" value="" />
						{$value.title}：</td>
					<td id="{$value.identifier}_price" class="text-danger">{$value.price}</td>
				</tr>
				<!-- /loop -->
				<tr>
					<td class="text-right">总价：</td>
					<td id="all_price" class="text-danger" width="150px">{$price}</td>
				</tr>
			</tbody>
		</table>
	</div>
	<!-- if $integral -->
	<div class="card mt-3">
		<div class="card-header">积分抵扣，仅支持100的整数倍，不支持小数</div>
		<ul class="list-group list-group-flush">
			<!-- loop from=$integral key=$key value=$value -->
		    <li class="list-group-item">
			    <div class="row">
				    <div class="col-sm-8">您当前的<span class="am-text-danger">{$value.title}</span>有 <span class="am-text-danger">{$value.val}</span>{$value.unit}，最多可抵现金<span class="red">{func price_format $value.price}</span></div>
				    <div class="col-sm-4">
					    <table>
					    	<tr>
					    		<td style="width:60px;text-align:right;">支出：</td>
					    		<td><input type="text" name="integral_val[{$key}]" class="form-control" data-name="integral" data-key="{$key}" /></td>
					    		<td>&nbsp;{$value.unit}</td>
					    	</tr>
					    </table>
				    </div>
			    </div>
		    </li>
		    <!-- /loop -->
		</ul>
	</div>
	<!-- /if -->
	<!-- if $balance -->
	<div class="card mt-3">
		<div class="card-header">余额支付</div>
		<div class="card-body">
			<!-- loop from=$balance key=$key value=$value -->
			<div class="form-check">
				<label class="form-check-label mb-2">
			    	<input type="radio" class="form-check-input" name="payment" value="{$value.identifier}" data-price="{$price_val}" data-balance="{$value.price}" />
			    	您当前的{$value.title}有<span class="red">{$value.val}</span>{$value.unit}
			    	<!-- if $price_val > $value.price -->
					，您的{$value.title}不够，请 <a href="{url ctrl=payment id=$value.id/}" target="_blank"><b>在线充值</b></a>
					<!-- /if -->
				</label>
			</div>
			<!-- /loop -->
		</div>
	</div>
	<!-- /if -->
	<!-- loop from=$paylist key=$key value=$value id=$tmpid -->
	<div class="card mt-3">
		<div class="card-header">{$value.title}</div>
		<div class="card-body">
			<!-- loop from=$value.paylist key=$k value=$v id=$idxx -->
			<div class="form-check">
				<label class="form-check-label mb-2">
			    	<input type="radio" class="form-check-input" name="payment" value="{$v.id}"{if !$tmpid.index && !$idxx.index} checked{/if} />
			    	<!-- if $v.logo1 -->
			    	<img src="{$v.logo1}" style="width:40px;"  />
			    	<!-- /if -->
			    	{$v.title}
				</label>
			</div>
			<!-- /loop -->
		</div>
	</div>
	<!-- /loop -->
	<div class="m-3 text-center">
		<input type="submit" value="提交订单" class="btn btn-primary btn-lg" />
	</div>
	</form>
</section>
<!-- include tpl=cart/footer -->